package org.yn.auth.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yn.auth.annotation.PermissionAnnotation;
import org.yn.auth.domain.Menu;
import org.yn.auth.domain.Role;
import org.yn.auth.dto.MenuDTO;
import org.yn.auth.dto.RoleDTO;
import org.yn.auth.service.IMenuService;
import org.yn.auth.service.IRoleService;
import org.yn.base.until.AjaxResult;
import org.yn.base.until.PageInfo;
import org.yn.query.MenuQuery;
import org.yn.query.RoleQuery;

import java.util.List;
import java.util.Objects;

@PermissionAnnotation(name="菜单管理",descs = "菜单管理")
@RestController
@RequestMapping("/menu")
@Api("菜单管理接口")
public class MenuController {


    @Autowired
    IMenuService menuService;

    /**
     * 新增
     */
    @PermissionAnnotation(name="新增或修改菜单",descs = "新增或修改菜单")
    @PostMapping()
    @ApiOperation(value = "新增或修改菜单")
    public AjaxResult updateOrInsert(@RequestBody Menu menu) {
        try {
            if (Objects.nonNull(menu)) {
                if (Objects.nonNull(menu.getId())) {
                    menuService.update(menu);
                } else {
                    menuService.insert(menu);
                }
            } else {
                return AjaxResult.error("请输入正确的数据");
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

    }

    @PermissionAnnotation(name="查询所有菜单",descs = "查询所有菜单")
    @GetMapping()
    @ApiOperation(value = "查询所有菜单")
    public AjaxResult selectAll() {
        try {
            List<Menu> menuList = menuService.selectAll();
            return AjaxResult.success(menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 查询单个菜单
     */
    @PermissionAnnotation(name="通过id查询菜单",descs = "通过id查询菜单")
    @GetMapping("/{id}")
    @ApiOperation(value = "通过id查询菜单")
    public AjaxResult selectById(@PathVariable Long id) {
        try {
            Menu menu = menuService.selectById(id);
            return AjaxResult.success(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }


    /**
     * 删除菜单
     */
    @PermissionAnnotation(name="通过id删除菜单",descs = "通过id删除菜单")
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过id删除菜单")
    public AjaxResult delete(@PathVariable Long id) {
        try {
            menuService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }



    /**
     * 分页查询接口
     */
    @PermissionAnnotation(name="菜单的分页查询",descs = "菜单的分页查询")
    @PutMapping
    @ApiOperation(value = "菜单的分页查询")
    public AjaxResult queryPage(@RequestBody MenuQuery menuQuery){
        try {
            PageInfo<Menu> pageInfo = menuService.queryPage(menuQuery);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 批量删菜单
     * @param ids
     * @return
     */
    @PermissionAnnotation(name="批量删除菜单",descs = "批量删除菜单")
    @PatchMapping
    @ApiOperation(value = "批量删除菜单")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            menuService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

// 获取菜单树
    @PermissionAnnotation(name="根据用户id获取菜单树",descs = "根据用户id获取菜单树")
    @GetMapping("/tree/{id}")
    @ApiOperation(value = "根据用户id获取菜单树")
    public AjaxResult queryMenuTree(@PathVariable("id") Long id) {
        try {
            List<Menu> list= menuService.queryMenuTree(id);
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    // 根据菜单添加保存角色
    @PermissionAnnotation(name="根据菜单添加保存角色",descs = "根据菜单添加保存角色")
    @PostMapping("/role")
    @ApiOperation(value = "根据菜单添加保存角色")
    public AjaxResult insertByMenuId(@RequestBody MenuDTO menuDTO) {
        try {
            menuService.insertByMenuId(menuDTO);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

    }
    //根据角色id查询对应的权限
    @PermissionAnnotation(name="根据菜单id查询对应的角色id",descs = "根据菜单id查询对应的角色id")
    @GetMapping("/role/{id}")
    @ApiOperation(value = "根据菜单id查询对应的角色id")
    public AjaxResult selectByMenuId(@PathVariable("id") Long id){
        try {
            List<Long> roleId = menuService.selectByMenuId(id);
            return AjaxResult.success(roleId);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

}
